import 'package:flutter/material.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/user_info.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/global_common.dart';
import 'package:flutter_syt/global_easy_loading.dart';
import 'package:flutter_syt/theme/font_styles.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/utils/utils_other.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

@RoutePage(name: "removeAccountConfirm")
class RemoveAccountConfirm extends StatefulWidget {
  final String code;
  const RemoveAccountConfirm(this.code);

  @override
  _RemoveAccountConfirmState createState() => _RemoveAccountConfirmState();
}

class _RemoveAccountConfirmState extends State<RemoveAccountConfirm> {
  TextStyle textStyle = FontStyles.headerTextStyle_16.copyWith(
    color: ThemeColor.colorBlack80,
    fontWeight: FontWeight.w300,
  );

  Future _destory() async {
    // try {
    if (await API.shared.user.destroy(widget.code)) {
      await GlobalData.unbindPush();

      // OnlineGlobalData.shared.clean();
      UserInfo.clean();
      Navigator.of(context).popUntil((route) => route.isFirst);
    }
    // } catch (e) {
    //   await UtilsOther.showCustomShowError(e);
    //   // if (e is DioError) {
    //   //   customShowError(e.message);
    //   // } else {
    //   //   customShowError(S().network_error);
    //   // }
    // }
  }

  Future _submit() async {
    try {
      await loading(_destory, text: S.of(context).deleting);
    } catch (e) {
      //  } catch (e) {
      await UtilsOther.showCustomShowError(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(S.of(context).confirm_delete),
      ),
      body: Container(
        color: Colors.white,
        padding: EdgeInsets.all(20),
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.only(top: 30),
              alignment: Alignment.center,
              child: Icon(
                Icons.error,
                color: Color(0xFFF55F5F),
                size: 60.0,
              ),
            ),
            Container(
              margin: EdgeInsets.only(bottom: 60, top: 24),
              child: RichText(
                textAlign: TextAlign.left,
                text: TextSpan(
                  children: [
                    TextSpan(
                      text: S.of(context).cancellation_prompt_2,
                    ),
                  ],
                  style: textStyle,
                ),
              ),
            ),
            Container(
              child: ElevatedButton(
                  child: Text(
                    S.of(context).confirm_delete,
                    style: TextStyle(fontWeight: FontWeight.bold),
                  ),
                  onPressed: ClickUtils.debounce(
                    () {
                      _submit();
                    },
                  )),
            ),
          ],
        ),
      ),
    );
  }
}
